﻿using System.Collections.Generic;
using System.Linq;
using System.Data.Objects;

namespace Sern.Common
{
    /// <summary>
    /// An open concrete implementation of IRepository interface. Resolution is deferred to the IoC container.
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <typeparam name="U"></typeparam>
    public class EFRepository<T, U> : IRepository<U>
        where T : ObjectContext
        where U : class
    {
        //private static IContextBag queryCache;
        private T _context;

        /// <summary>
        /// Creates an EFRepository based on the context.
        /// </summary>
        /// <param name="context"></param>
        public EFRepository(T context /*, IContextBag something*/)
        {
            _context = context;

        }

        /// <summary>
        /// Get the records.
        /// </summary>
        public virtual IQueryable<U> Records
        {
            get
            {
                return _context.CreateObjectSet<U>();
            }
        }

        /// <summary>
        /// Add a number of new entities to the repository.
        /// </summary>
        /// <param name="set">A set of entities to add.</param>
        public virtual void AddMany(IEnumerable<U> set)
        {
            ObjectSet<U> table = _context.CreateObjectSet<U>();
            foreach (U member in set)
            {
                table.AddObject(member);
            }
        }

        /// <summary>
        /// Adds a single entity to the context.
        /// </summary>
        /// <param name="entity"></param>
        public virtual void Add(U entity)
        {
            _context.CreateObjectSet<U>().AddObject(entity);
        }

        /// <summary>
        /// Deletes many entities from context.
        /// </summary>
        /// <param name="set">A set of entities to delete.</param>
        public virtual void DeleteMany(IEnumerable<U> set)
        {
            UpdateMany(set);
            ObjectSet<U> table = _context.CreateObjectSet<U>();
            foreach (U member in set)
            {
                table.DeleteObject(member);
            }
        }

        /// <summary>
        /// Delete a single entity from context.
        /// </summary>
        /// <param name="entity">The entity to be deleted.</param>
        public virtual void Delete(U entity)
        {
            Update(entity);
            _context.CreateObjectSet<U>().DeleteObject(entity);
        }

        /// <summary>
        /// Apply changes.
        /// </summary>
        /// <param name="set"></param>
        public virtual void UpdateMany(IEnumerable<U> set)
        {
            ObjectSet<U> table = _context.CreateObjectSet<U>();
            foreach (U member in set)
            {

                if (!table.IsAttached(member))
                {
                    table.ApplyCurrentValues(member);
                }
            }
        }

        /// <summary>
        /// Apply changes.
        /// </summary>
        /// <param name="set"></param>
        public virtual void Update(U entity)
        {
            ObjectSet<U> table = _context.CreateObjectSet<U>();
            if (!table.IsAttached(entity))
            {
                table.ApplyCurrentValues(entity);
            }
        }

        /// <summary>
        /// Persist changes to the database.
        /// </summary>
        public virtual void Save()
        {
            _context.SaveChanges();
        }
    }
}
